package com.cpe.web.controller.appoint;

import static com.cpe.data.domain.student.StudentRespCode.SEND_MESSAGE_FAIL;
import static com.sql.data.domain.user.UserRespCode.INVALID_TOKEN;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cpe.data.domain.member.Member;
import com.cpe.data.domain.member.MemberRespCode;
import com.cpe.data.domain.member.appoint.MemberAppoint;
import com.cpe.data.domain.member.appoint.MemberAppointForm;
import com.cpe.data.domain.student.Student;
import com.cpe.repository.mapper.member.MemberAppointMapper;
import com.cpe.repository.mapper.member.MemberMapper;
import com.cpe.repository.mapper.student.StudentMapper;
import com.cpe.transfer.member.MemberAppointTransfer;
import com.cpe.transfer.student.StudentTransfer;
import com.sql.data.response.CommonRespCode;
import com.sql.data.response.JsonResponse;

import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/member_appoints")
public class MemberAppointController {

	@Resource
	private MemberMapper memberMapper;

	@Resource
	private MemberAppointTransfer memberAppointTransfer;

	@Resource
	private MemberAppointMapper memberAppointMapper;

	@Resource
	private StudentMapper studentMapper;

	@Resource
	private StudentTransfer studentTransfer;

	/**
	 * 指定一个学生预约指定的项目
	 */
	@RequestMapping(method = POST)
	public JsonResponse addMemberAppoint(@RequestBody MemberAppointForm form, HttpSession session,
			@RequestHeader String openid) {
		// 登录校验
		Member member = (Member) session.getAttribute("loginer");
		if (member == null) {
			// 尝试从Header里读取openid
			member = memberMapper.findByOpenid(openid);
			if (member == null) {
				return JsonResponse.custom(INVALID_TOKEN);
			} else {
				form.setMemberId(member.getId());
			}
		} else {
			form.setMemberId(member.getId());
		}

		// 参数校验
		if (form.stopCreate()) {
			return JsonResponse.invalid("缺少必输项！");
		}

		// 数据库校验
		Student student = null;
		if (StringUtil.isBlank(form.getStudentId())) {
			List<Student> students = studentMapper.findByParentId(member.getId());
			if (students.size() == 0) {
				return JsonResponse.custom(MemberRespCode.NO_CHILD_FOUND);
			}
			if (students.size() > 1) {
				return JsonResponse.custom(MemberRespCode.MANY_CHILDS_FOUND).data(studentTransfer.toInfoList(students));
			}
			student = students.get(0);
			form.setStudentId(student.getId());
		} else {
			student = studentMapper.findById(form.getStudentId());
			if (student == null) {
				return JsonResponse.custom(CommonRespCode.INVALID_PARAMETER);
			}
		}

		MemberAppoint memberAppoint = new MemberAppoint();
		memberAppoint.setAppointId(form.getAppointId());
		memberAppoint.setStudentId(form.getStudentId());
		memberAppoint = memberAppointMapper.findOne(memberAppoint);
		if (memberAppoint != null) {
			return JsonResponse.custom(MemberRespCode.ALREADY_APPOINT_FOR_CHILD);
		}

		try {
			memberAppoint = memberAppointTransfer.toEntity(form);
			memberAppointMapper.insert(memberAppoint);
			memberAppoint.setStudentName(student.getName());
			return JsonResponse.ok(memberAppoint);
		} catch (Exception e) {
			log.info(e.getMessage(), e);
			return JsonResponse.custom(SEND_MESSAGE_FAIL);
		}
	}
}
